1
|
|
|
'use strict' |
2
|
|
|
|
3
|
|
|
import joi from 'joi' |
4
|
|
|
import logger from 'winston' |
5
|
|
|
import config from '../../config' |
6
|
|
|
import { mailerSender } from './senders' |
7
|
|
|
import { userLanguageObject } from '../helpers/i18n' |
8
|
|
|
import * as jwt from 'jsonwebtoken' |
9
|
|
|
|
10
|
|
|
let options = {} |
11
|
|
|
|
12
|
|
|
const createToken = (payload) => { |
13
|
|
|
return jwt.sign(payload, config.jwt.secret, { |
14
|
|
|
expiresIn: `${options.jwt.tokenLifeSpan}d` |
15
|
|
|
}) |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
const checkOptions = (input) => { |
19
|
|
|
const schemaOptions = joi.object().keys({ |
20
|
|
|
req: joi.object().keys({ |
21
|
|
|
isSecure: joi.func(), |
22
|
|
|
headers: joi.object().keys({ |
23
|
|
|
host: joi.string() |
24
|
|
|
}).required() |
25
|
|
|
}).required() |
26
|
|
|
}) |
27
|
|
|
const { error } = joi.validate(input, schemaOptions, { allowUnknown: true }) |
28
|
|
|
return error |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// implementation of notification send |
32
|
|
|
const sendNotification = (record, action, done) => { |
33
|
|
|
done((opts) => { |
34
|
|
|
if (!checkOptions(opts)) { |
35
|
|
|
if (!options.i18n) { |
36
|
|
|
throw new Error(`Error: mailers, bad configuration found: i18n not found`) |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
const host = opts.req.isSecure() |
40
|
|
|
? 'https' |
41
|
|
|
: 'http' + '://' + opts.req.headers.host |
42
|
|
|
|
43
|
|
|
let token = '' |
44
|
|
|
let subject = '' |
45
|
|
|
let url = '' |
46
|
|
|
|
47
|
|
|
// sets the correct language for emplate |
48
|
|
|
const i18n = userLanguageObject({ user: record }) |
49
|
|
|
|
50
|
|
|
switch (action) { |
51
|
|
|
case 'account_confirmation': |
52
|
|
|
token = createToken({ confirmationToken: record.confirmationToken }) |
53
|
|
|
url = `${host}/confirmation` |
54
|
|
|
subject = i18n.t('mailer.account_confirmation.subject') |
55
|
|
|
break |
56
|
|
|
|
57
|
|
|
case 'password_recovery': |
58
|
|
|
token = createToken({ recoveryToken: record.recoveryToken }) |
59
|
|
|
url = `${host}/change_password` |
60
|
|
|
subject = i18n.t('mailer.password_recovery.subject') |
61
|
|
|
break |
62
|
|
|
|
63
|
|
|
case 'account_recovery': |
64
|
|
|
token = createToken({ unlockToken: record.unlockToken }) |
65
|
|
|
url = `${host}/unlock` |
66
|
|
|
subject = i18n.t('mailer.account_recovery.subject') |
67
|
|
|
break |
68
|
|
|
|
69
|
|
|
default: |
70
|
|
|
logger.warn('mailer', 'Template not found') |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// send mail |
74
|
|
|
mailerSender(action, { |
75
|
|
|
i18n, |
76
|
|
|
subject, |
77
|
|
|
email: record.email, |
78
|
|
|
url: `${url}?token=${token}` |
79
|
|
|
}) |
80
|
|
|
} |
81
|
|
|
}) |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
export default function (schema, opt) { |
85
|
|
|
options = Object.assign({ |
86
|
|
|
jwt: { |
87
|
|
|
tokenLifeSpan: 3 |
88
|
|
|
} |
89
|
|
|
}, opt) |
90
|
|
|
|
91
|
|
|
// TODO |
92
|
|
|
// https://github.com/Automattic/kue |
93
|
|
|
// https://github.com/OptimalBits/bull |
94
|
|
|
|
95
|
|
|
// implementation of notification send |
96
|
|
|
schema.methods.send = sendNotification |
97
|
|
|
} |
98
|
|
|
|